﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.member
{
    public partial class settings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userInfo = WebMasterCookie.GetCookie();
            if (userInfo == null)
            {
                Response.Redirect("/login.aspx");
            }
            AjaxPro.Utility.RegisterTypeForAjax(typeof (settings));
        }

        //查询用户信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetUserInfo()
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            var userInfo = WebMasterCookie.GetCookie();
            if (userInfo != null)
            {
                try
                {
                    var table = ClientInfoBll.GetClientInfoTable("Client_Id=" + userInfo.Id, "Client_Id asc", CommonConst.DB_Name);
                    if (table.Rows.Count > 0)
                    {
                        jsonResult.Add("account", table.Rows[0]["Client_Account"]);
                        jsonResult.Add("username", table.Rows[0]["Client_Name"]);
                        jsonResult.Add("email", table.Rows[0]["User_Email"]);
                        jsonResult.Add("cellphone", table.Rows[0]["User_CellPhone"]);
                        jsonResult.Add("telphone", table.Rows[0]["User_TelPhone"]);
                        jsonResult.Add("contact", table.Rows[0]["User_Contact"]);
                        jsonResult.Add("url", table.Rows[0]["User_Url"]);
                        jsonResult.Add("address", table.Rows[0]["User_Address"]);
                        jsonResult.Add("qq", table.Rows[0]["User_QQ"]);
                        jsonResult.Add("isreload", true);
                    }
                    else
                    {
                        jsonResult.Add("isreload", false);
                        jsonResult.Add("msg", "非法用户，请退出重新登录！");
                    }
                }
                catch (Exception ex)
                {
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }

            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //保存用户信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string SaveUserInfo(string json)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            var userInfo = WebMasterCookie.GetCookie();
            if (userInfo != null)
            {
                var dicProj = JsonHelper.JSONToObject<Dictionary<string, object>>(json);
                if (dicProj["pwd"].ToString()!="")
                {
                    dicProj["pwd"] = EncryptHelper.EncryptString(dicProj["pwd"].ToString());
                }
                ClientInfoBll.UserInfoModify(dicProj, userInfo.Id, CommonConst.DB_Name);
                //更新cookie
                userInfo.RelName = dicProj["username"].ToString();
                userInfo.Email = dicProj["email"].ToString();
                WebMasterCookie.UpdateCookie(userInfo);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", "完善客户基本信息");
                dicHandleLog.Add("obj_guid", "Client_Id=" + userInfo.Id);
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);

                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", "用户信息修改成功！");
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
